<template>
  <div>
    <img class="icon-box" src="@/assets/setting/favicon.png" @click.stop="showConsole">
    <p class="welcome-tips">欢迎使用应用模板</p>
    <div class="login-box">正在登录 <inline-loading /></div>
  </div>
</template>

<script>
import { InlineLoading } from 'vux';

// 使用cookies 存储 token
import { setToken } from '@/utils/auth';

// 使用jssdk存储至豆豆客户端
// import { setToken } from '@/utils/auth-jssdk';

import { thirdPartyLogin, loginApi } from '@/api/login';

export default {
  components: {
    InlineLoading,
  },
  data() {
    return {
      count: 0,
      lastClickTime: 0,
    };
  },
  async created() {
    const code = this.getRequest('code');
    const dood = this.getUrlParam('dood');
    console.log(code, '从工作台中获取的Code');
    if (code) {
      this.byCodeLogin({ code: code });
    }
    if (dood) {
      this.byCodeLogin({ dood: dood });
    }
    // 本地调试模拟 dood 登录，打开使用
    // this.login();
  },
  methods: {
    // 解析code
    getRequest(key) {
      const url = location.search;
      const theRequest = {};
      if (url.indexOf('?') !== -1) {
        const str = url.substr(1);
        const strs = str.split('&');
        for (let i = 0; i < strs.length; i++) {
          theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
        }
        const value = theRequest[key];
        return value;
      }
    },
    // 解析dood
    getUrlParam(paraName) {
      const url = document.location.toString();
      const arrObj = url.split('?');
      if (arrObj.length > 1) {
        const arrPara = arrObj[1].split('&');
        let arr;
        for (let i = 0; i < arrPara.length; i++) {
          arr = arrPara[i].split('=');
          if (arr != null && arr[0] === paraName) {
            return arr[1];
          }
        }
        return '';
      } else {
        return '';
      }
    },
    async byCodeLogin(params) {
      const result = await thirdPartyLogin(params);
      if (result.status === 200) {
        const token = result.headers['authorization'];
        await setToken(token);
        setTimeout(() => {
          this.$router.replace('/home');
        }, 800);
      }
    },
    // 本地调试代码
    login() {
      const params = {
        username: 'test',
        password: window.btoa(123456),
      };
      loginApi(params).then(async(res) => {
        this.$logan.log(JSON.stringify(res));
        const token = res.headers['authorization'];
        await setToken(token);
        this.$logan.log('login success');
        setTimeout(() => {
          this.$router.replace('/home');
        }, 800);
      });
    },
    showConsole() {
      const nowTime = new Date().getTime();
      if (this.count === 0) {
        this.lastClickTime = nowTime;
        this.count++;
      } else if (nowTime - this.lastClickTime < 3000) {
        this.lastClickTime = nowTime;
        this.count++;
      } else {
        this.count = 0;
      }

      if (this.count >= 6) {
        const element = document.getElementById('__vconsole');
        element.style.display = 'block';
      }
    },
  },
};
</script>

<style scoped>
.icon-box {
  display: block;
  margin: 0 auto;
  margin-top: 160px;
}
.welcome-tips {
  text-align: center;
  color: #666;
}
.login-box {
  position: absolute;
  color: #666;
  left: 0;
  right: 0;
  bottom: 160px;
  text-align: center;
}
</style>
